<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script>
var fn = {
		isTab: false,
		className: "${className}",
		/**
		*提交时动态参数
		*/
		queryParams: {},
		data: null,
		/**
		*表单id
		*/
		formId : "#easyuiForm",
		/**
		* 加载初始化数据
		*/
		loadData:function(data){
			if(data) $(this.formId).form("load",data );
		},
		/**
		*获取当前对话框的data数据
		*/
		getDialogData: function(){
			var data = null;
			if(frameElement && frameElement.dialog){
				var dialog =  frameElement.dialog, data = dialog.dialog("get","data") || null ;
			}
			if(this.isTab){
				data =  parent.consoleFn.getTabData();
			}
			return data;
		},
		
		getDialog: function(){
			var dialog = null;
			if(frameElement && frameElement.dialog){
				dialog =  frameElement.dialog;
			}
			return dialog;
		},
		
		/**
		*清空表单
		*/
		clearForm:function(){
			$(this.formId).form('clear');
		},
		onAfterResetForm:null,
		/**
		*@desc combobox控件加载 fieldClassName和modleName根据使用情况二选一
		*@paramArray 
		*	[
		* 	 	{
		*			url : '' , 			//url参数对象为可选参数，如果没有自己实现特殊数据封装，则采用默认请求地址，ulr可不指定
		*			field: '#gender', 	//html对象
		*			fieldClassName: 'com.fs.base.pojo.Dictionary', //加载对象方式1
		*			modleName: 'dictionary',						 //加载对象方式2
		*			valueField:'dicName',   //值域字段 
		*		    textField:'dicName',	//文本域字段
		*		    queryParams:{
		*				pCode: '0000',			//查询条件
		*		    	pCode_searchType: 'eq',	//查询方式
		*		    	text:'男'			//默认文本值
					}
		*		}
		* 	]
		*/
		combobox: function(paramArray){
			var _this = this;
			paramArray = paramArray || [] ;
			$.each(paramArray,function(index,item){
				var valueField = item.valueField || null ;
				var textField = item.textField || null;
				var url = item.url || null; 
				var modleName = item.modleName || "" ;
				var queryParams = $.extend(item.queryParams || {},{modleName:modleName  });
				if(item.field){
					var url = (url) ? url : ("/console/combobox") ;
					var param = {url: url ,queryParams: queryParams };
					if(valueField) param.valueField = valueField;  
					if(textField) param.textField = textField;  
					$(item.field).combobox(param);  
				}
			});
		},
		/**
		*初始化或重置表单
		*/
		resetForm:function(param){
			if(frameElement && frameElement.dialog){
				var dialog =  frameElement.dialog, data = dialog.dialog("get","data") || {} ;
				//console.log(dialog);
				//console.log(data);
				//console.log(data.o);
				if(data.o){
					data = $.extend(data,data.o);
				}
				data = $.extend({},data,param || {} ) ;
				if(param && param.formId){
					this.formId = param.formId;
				}
				this.loadData(data);
				this.data = data ;
			}
			if(this.isTab){
				if(param && param.formId){
					this.formId = param.formId;
				}
				var data = parent.consoleFn.getTabData();
				this.loadData(data);
				this.data = data ;
			}
			var onAfterResetForm = this.onAfterResetForm;
			if(onAfterResetForm!=null) onAfterResetForm.call(this);
		},
		readOnly:function(){
			$(this.formId + " :input,select,textarea").attr("readonly", "readonly"); 
		},
		onAfterSubmitForm:null,
		onBeforeSubmitForm:null, //此函数返回false时取消提交
		submitForm:function(){
			var _this = this;
			var onBeforeSubmitForm = this.onBeforeSubmitForm;
			if(onBeforeSubmitForm!=null){
				var flag = onBeforeSubmitForm.call(this)
				if(flag==false) return;
			}
			$(this.formId).form('submit',{
				iframe: false,
				queryParams: this.queryParams || {} ,
				onSubmit:function(param){
					return $(this).form('enableValidation').form('validate');
				},
				success:function(data){
					var json = JSON.parse(data);
					if(json && json.flag){
						var onAfterSubmitForm = _this.onAfterSubmitForm;
						if(onAfterSubmitForm){
							onAfterSubmitForm.call(this);
						}
						if(_this.isTab && parent && parent.consoleFn){
							parent.consoleFn.closeTabAndFrushGrid();
						}
						if(parent.p&& parent.p.refreshParent) parent.p.refreshParent(json.msg);
					}else{
						alert(json.msg || '后台处理失败！');
					}
				}
			});
		},
		/**设置项目部名称*/
		selectDepartmant :function(rows){
			var text = (rows && rows.length>=1) ? (rows[0].department || '') : '';
			if(text!=''){
				$('#department').combobox('setValue', text);
			}
		},
		departmentFilter:function(data){
			//var resultData = [];
			//var dataRight = "{SESSION_USER.dataRight}";
			////var departs = "{SESSION_USER.departments}" || [];
			//if(dataRight==3 || dataRight==1){
			//	var item = null;
			//	for(var i=0;i<data.length;i++){
			//		item = data[i];
			//		if(departs.indexOf(item.text)>-1){
			//			resultData.push(item);
			//		}
			//	}
			//	return resultData;
			//}*/
			return data;
		},
		bindTabButton: function(buts){
			var _this = this;
			_this.isTab = true;
			$(buts).on("click",function(){
				var $this = $(this), id = $(this).attr("id");
				switch (id) {
				case 'f_btn_submit':
					$this.linkbutton('disable');
					setTimeout(function(){
						$this.linkbutton('enable');
					},1000);
					fn.submitForm();
					break;
				case 'f_btn_close':
					parent.consoleFn.closeTab();
					break;
				default:
					break;
				}
			});
		}
};
</script>